using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BanHangCshap.Class;
using BanHangCshap.Report;
namespace BanHangCshap
{
    public partial class frmThongKeKhachHangDuCT : DevExpress.XtraEditors.XtraForm
    {
        KhachHangNCC clskhncc = new KhachHangNCC();
        public frmThongKeKhachHangDuCT()
        {
            InitializeComponent();
        }
        public void loadKhachHangNCC(DevExpress.XtraEditors.GridLookUpEdit glku)
        {
            DataTable dt = clskhncc.layDSKhachHangNCCTheoLoai(1);
            DataRow dr = dt.NewRow();
            dr[0] = "";
            dr[1] = "";
            dr[2] = "";
            dt.Rows.InsertAt(dr, 0);
            glku.Properties.DataSource = dt;
            glku.Properties.ValueMember = "makhncc";
            glku.Properties.DisplayMember = "khachhangncc";
            glku.Properties.PopupFormWidth = 600;


        }
        public void loadDSDotKhuyenMai()
        {
            DotKhuyenMai clskm = new DotKhuyenMai();
            DataTable dt = clskm.layDSDotKhuyenMai();
            DataRow dr = dt.NewRow();
            dr[0] = dr[1] = "";
            dt.Rows.InsertAt(dr, 0);
            glkuDotKhuyenMai.Properties.DataSource = dt;
            glkuDotKhuyenMai.Properties.ValueMember = "madot";
            glkuDotKhuyenMai.Properties.DisplayMember = "tendot";
            glkuvDotKhuyenMai.BestFitColumns();
        }


        private void frmThongKeKhachHangDuCT_Load(object sender, EventArgs e)
        {
            loadKhachHangNCC(glkuKHNCCTK);
            loadDSDotKhuyenMai();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            string sql = "select (select tendoanhnghiep from ThongTinSuDung) as tendoanhnghiep,(select diachi from ThongTinSuDung) as diachidoanhnghiep,(select dienthoai from ThongTinSuDung) as dienthoaidoanhnghiep,a.madot, tendot, convert(varchar(10),ngaybd,103) as ngaybd, convert(varchar(10),ngaykt,103) as ngaykt, doanhso, a.ghichu,  "; 
            sql += "c.makhncc + ' - ' + tenkhncc as khachhang, diachikhncc, sodtkhncc, ";
            sql += "b.manhapxuat, convert(varchar(10),ngaynhapxuat,103) as ngaynhapxuat, b.mahang, tenhang,b.dvt, slchuaquydoi, km, dgnhapxuat, chietkhau, thanhtien  "; 
            sql += "from DotKhuyenMai a left join PhieuNhapXuatCT b on a.madot = b.madot left join PhieuNhapXuat c on b.manhapxuat = c.manhapxuat "; 
            sql += "left join KhachHangNCC d on c.makhncc = d.makhncc left join HangHoa e on b.mahang = e.mahang ";
            sql += "where loai = 4 and (select count(mahang) from PhieuNhapXuatCT where madot = a.madot) > 0 and kmtheodoanhso = 'true' ";
            if (glkuDotKhuyenMai.Text.Trim().Length > 0)
                sql += "and a.madot = '" + glkuDotKhuyenMai.EditValue.ToString().Trim() + "' and c.makhncc <> '' ";
            if (glkuKHNCCTK.Text.Trim().Length > 0)
                sql += "and c.makhncc = '" + glkuKHNCCTK.EditValue.ToString().Trim() + "'";
            sql += "order by a.madot, c.makhncc, b.manhapxuat";

            rptThongKeTongHopKhachHangDuCT rpt = new rptThongKeTongHopKhachHangDuCT();
            rpt.SetDataSource(clskhncc.getDataTable(sql));
            crv.ReportSource = rpt;
        }
    }
}